clear all 
clear global

global folderfig foldermats day keepall largetrade quantityrobus  tradesize homedealer

%paths
folderfig = '/Users/wittwer/Dropbox/5_Jason and Milena/data/Matlab_JMP_2R1/main_estimation_submission2R1/3_figures/';
foldermats= '/Users/wittwer/Dropbox/5_Jason and Milena/data/Matlab_JMP_2R1/main_estimation_submission2R1/2_output/';


%%Parameters to get Table (a) Benchmark model with loyalty benefits
homedealer=1;
largetrade=0;
tradesize=0;
zerobenefit=0;

%%%% Parameters to get Table (b) Benchmark model with loyalty benefits
%homedealer=0;
%largetrade=0;
%tradesize=0;
%zerobenefit=1;

%%%% Parameters to get Table (c) Extended model with small and large trades 
%homedealer=1;
%largetrade=1;
%tradesize=0; %0 for small trades, 1 for large trades
%zerobenefit=0;

day=1;keepall=1;quantityrobus=0;fej=1;


plot=1;  %no plots
for side=[1]
    for retailer=0

    if zerobenefit==0
    temp=['/estimation',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
    else
        temp=['/estimation',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(0), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize),'_',num2str(fej) , '_zerobenefit']];
    end 

    %For plots:
    if plot==1
        input_mat_file=fullfile(foldermats,temp);
       table = plot_estimation_results2(input_mat_file, retailer, side, 0);
    end 

    %For tables:
    load([foldermats,temp,'.mat'])

    nanmedian(Mhat)
    nanmedian(SE)

    %Investor's total value
     if homedealer==1 
         if retailer==0
             probH = nanmean(sHjSQ + rhojSQ,2);
             if side==1
                valueI = Mhat(:,1)+nanmean(xij_data,2) - (Y(1,24)-Y(1,23));
                valueIB= Mhat(:,1)+nanmean(xij_data,2);
                value = probH.* valueI + (ones(T,1) -probH ).*valueIB;
            else 
                valueI  = Mhat(:,1)-nanmean(xij_data,2) + (Y(1,24)-Y(1,23));
                valueIB = Mhat(:,1)-nanmean(xij_data,2);
                 value = probH.* valueI + (ones(T,1) -probH ).*valueIB;
             end 

               disp(['The investors value when trading with another dealer is:' , num2str(nanmedian(valueIB))])
                disp(['The investors value when trading with a home dealer is:' , num2str(nanmedian(valueI))])
                disp(['The investors value when trading (ex-ante):' , num2str(nanmedian(value))])

         else 
            if side==1
                valueI = Mhat(:,1)+nanmean(Y(new_period(:,2), 23),2) - (Y(1,24)-Y(1,23));
            else 
                valueI = Mhat(:,1)-nanmean(Y(new_period(:,2), 23),2) + (Y(1,24)-Y(1,23));
            end 
                disp(['The investors value when trading with a home dealer is:' , num2str(nanmedian(valueI))])
        end 
        end 

    if retailer==0
    if side==1
       valueD =  mean(nanmedian(vdjSQ(:,cols)-Y(new_period(:,2),5)));
    else
            valueD = mean(nanmedian(Y(new_period(:,2),5)-vdjSQ(:,cols)));
    end 
     disp(['The dealers value is:' , num2str(valueD)])
    %mean(nanmedian(lvdjSQ(:,cols)))
     disp(['The platform elasticity is: ' , num2str(mean(nanmedian(etaEjSQ)))])
    %mean(nanmedian(etaEjSQ))
    %mean(nanmean(etaDjSQ))
    %mean(nanmedian(xij_data))
    end
    
    
 

    end 
end 
